package org.androidpn.activity;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.R;
import org.androidpn.adapter.TagGridViewAdapter;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CHotTag;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.AppCache;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;
import org.androidpn.widget.ClearEditText;
import org.androidpn.widget.NoScrollGridView;

import com.android.volley.VolleyError;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class ModifyUserInfoActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	private ClearEditText etModifyinfo;
	private ImageView ivBack;
	private TextView tvType;
	private TextView tvSave;

	private RadioGroup radioGroup;
	private RadioButton radioButton_man;
	private RadioButton radioButton_woman;

	private CUserInfo cUserInfo;

	private int modifyType = 1;

	private LinearLayout tagContent;
	List<CHotTag> alltags = new ArrayList<CHotTag>();
	List<CHotTag> mytags = new ArrayList<CHotTag>();
	TagGridViewAdapter adapter1;
	TagGridViewAdapter adapter2;
	NoScrollGridView gridView1;
	NoScrollGridView gridView2;

	private static final int MSG_SHOW_PROGRESSE_DIALOG = 0x200;
	private static final int MSG_HIDE_PROGRESSE_DIALOG = 0x201;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.modify_userinfo_activity);
		etModifyinfo = (ClearEditText) findViewById(R.id.et_modifyinfo);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		tvType = (TextView) findViewById(R.id.tv_modifytype);
		tvSave = (TextView) findViewById(R.id.tv_save);
		radioButton_man = (RadioButton) findViewById(R.id.rd_man);
		radioButton_woman = (RadioButton) findViewById(R.id.rd_woman);
		radioGroup = (RadioGroup) findViewById(R.id.radio_group);
		cUserInfo = (CUserInfo) getIntent().getSerializableExtra("cuserinfo");
		modifyType = getIntent().getIntExtra("modifytype", ConstanceUtil.MODIFY_TYPE_NAME);
		etModifyinfo.setInputType(InputType.TYPE_CLASS_TEXT);

		tagContent = (LinearLayout) findViewById(R.id.tag_content);
		gridView1 = (NoScrollGridView) findViewById(R.id.gridview1);
		gridView2 = (NoScrollGridView) findViewById(R.id.gridview2);

		radioGroup.setVisibility(View.GONE);
		tagContent.setVisibility(View.GONE);
		etModifyinfo.setVisibility(View.VISIBLE);
		gridView1.setOnItemClickListener(this);
		gridView2.setOnItemClickListener(this);
		adapter1 = new TagGridViewAdapter(this);
		adapter1.setTags(mytags);
		adapter2 = new TagGridViewAdapter(this);
		adapter2.setTags(alltags);
		gridView1.setAdapter(adapter1);
		gridView2.setAdapter(adapter2);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
				int checkedid = arg0.getCheckedRadioButtonId();
				if (checkedid == R.id.rd_man) {
					cUserInfo.setSex(ConstanceUtil.TYPE_SEX_MAN);
				} else {
					cUserInfo.setSex(ConstanceUtil.TYPE_SEX_WOMAN);
				}
			}
		});

		if (modifyType == ConstanceUtil.MODIFY_TYPE_NAME) {
			tvType.setText("姓名");
			etModifyinfo.setText(cUserInfo.getName());
		} else if (modifyType == ConstanceUtil.MODIFY_TYPE_AGE) {
			etModifyinfo.setInputType(InputType.TYPE_CLASS_NUMBER);
			tvType.setText("年龄");
			etModifyinfo.setText(cUserInfo.getAge() + "");
		} else if (modifyType == ConstanceUtil.MODIFY_TYPE_SEX) {
			tvType.setText("性别");
			radioGroup.setVisibility(View.VISIBLE);
			etModifyinfo.setVisibility(View.GONE);
			tagContent.setVisibility(View.GONE);
			if (cUserInfo.getSex() == ConstanceUtil.TYPE_SEX_WOMAN) {
				radioButton_woman.setChecked(true);
			} else {
				radioButton_man.setChecked(true);
			}
		} else if (modifyType == ConstanceUtil.MODIFY_TYPE_HOSPITAL) {
			tvType.setText("所属医院");
			etModifyinfo.setText(cUserInfo.getHospital());
		} else if (modifyType == ConstanceUtil.MODIFY_TYPE_MAJOR) {
			tvType.setText("专科");
			etModifyinfo.setText(cUserInfo.getMajor());
		} else if (modifyType == ConstanceUtil.MODIFY_TYPE_DESCRIPTION) {
			tvType.setText("班次");
			etModifyinfo.setText(cUserInfo.getDescription());
		} else if (modifyType == ConstanceUtil.MODIFY_TYPE_TAG) {
			tvType.setText("标签");
			radioGroup.setVisibility(View.GONE);
			etModifyinfo.setVisibility(View.GONE);
			tagContent.setVisibility(View.VISIBLE);
		}
		GetAllTagResponseListenner listener = new GetAllTagResponseListenner();
		HttpApi.getAllHotTagList(listener, listener);
		etModifyinfo.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
			}
		});
		ivBack.setOnClickListener(this);
		tvType.setOnClickListener(this);
		tvSave.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.tv_save:
			// 保存发送服务器
			if (modifyType != ConstanceUtil.MODIFY_TYPE_SEX && modifyType != ConstanceUtil.MODIFY_TYPE_TAG) {
				if (TextUtils.isEmpty(etModifyinfo.getText().toString())) {
					ToastUtil.showShortToast(this, "修改的信息不能为空");
					return;
				}
			}
			if (modifyType == ConstanceUtil.MODIFY_TYPE_NAME) {
				cUserInfo.setName(etModifyinfo.getText().toString());
			} else if (modifyType == ConstanceUtil.MODIFY_TYPE_AGE) {
				int age = Integer.parseInt(etModifyinfo.getText().toString());
				cUserInfo.setAge(age);
			} else if (modifyType == ConstanceUtil.MODIFY_TYPE_SEX) {
				// tvType.setText("性别");
			} else if (modifyType == ConstanceUtil.MODIFY_TYPE_HOSPITAL) {
				// tvType.setText("医院");
				cUserInfo.setHospital(etModifyinfo.getText().toString());
			} else if (modifyType == ConstanceUtil.MODIFY_TYPE_MAJOR) {
				// tvType.setText("专业");
				cUserInfo.setMajor(etModifyinfo.getText().toString());
			} else if (modifyType == ConstanceUtil.MODIFY_TYPE_DESCRIPTION) {
				cUserInfo.setDescription(etModifyinfo.getText().toString());
			} else if (modifyType == ConstanceUtil.MODIFY_TYPE_TAG) {
				String tags = "";
				for (int i = 0; i < mytags.size(); i++) {
					if (i == 0) {
						tags += mytags.get(i).getTag();
					} else {
						tags += (ConstanceUtil.TAG + mytags.get(i).getTag());
					}
				}
				cUserInfo.setTag(tags);
			}
			// 显示提示框
			sendEmptydMessage(MSG_SHOW_PROGRESSE_DIALOG);
			MyResponseListener responseListener = new MyResponseListener();
			HttpApi.saveUserinfo(cUserInfo, responseListener, responseListener);
			break;
		}
	}

	class MyResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onResponse(String response) {
			// 隐藏提示框
			sendEmptydMessage(MSG_HIDE_PROGRESSE_DIALOG);
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				ToastUtil.showShortToast(ModifyUserInfoActivity.this, netModel.getContent());
				Intent intent = new Intent();
				intent.putExtra("cuserinfo", cUserInfo);
				AppCache.getInstance().saveUserInfo(cUserInfo);
				setResult(RESULT_OK, intent);
				ModifyUserInfoActivity.this.finish();
			} else {
				ToastUtil.showShortToast(ModifyUserInfoActivity.this, netModel.getContent());
			}
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			// 隐藏提示框
			sendEmptydMessage(MSG_HIDE_PROGRESSE_DIALOG);
			ToastUtil.showShortToast(ModifyUserInfoActivity.this, "网络异常");
		}
	}

	class GetAllTagResponseListenner
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			ToastUtil.showShortToast(ModifyUserInfoActivity.this, "网络异常");
		}

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			System.out.println("tagsinfo:" + response);
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				alltags = JsonUtil.parseArray(netModel.getContent(), CHotTag.class);
				if (!TextUtils.isEmpty(cUserInfo.getTag())) {
					for (int i = 0; i < alltags.size(); i++) {
						if (cUserInfo.getTag().contains(alltags.get(i).getTag().trim())) {
							mytags.add(alltags.get(i));
						}
					}
					alltags.removeAll(mytags);
					adapter1.setTags(mytags);
					adapter1.notifyDataSetChanged();
					adapter2.setTags(alltags);
					adapter2.notifyDataSetChanged();
				} else {
					adapter1.setTags(mytags);
					adapter1.notifyDataSetChanged();
					adapter2.setTags(alltags);
					adapter2.notifyDataSetChanged();
				}

			}

		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (arg0.getId()) {
		case R.id.gridview1:
			alltags.add(mytags.get(arg2));
			mytags.remove(arg2);
			adapter1.notifyDataSetChanged();
			adapter2.notifyDataSetChanged();
			break;
		case R.id.gridview2:
			mytags.add(alltags.get(arg2));
			alltags.remove(arg2);
			adapter1.notifyDataSetChanged();
			adapter2.notifyDataSetChanged();
			break;
		}

	}

	@Override
	protected void onHandleMessage(Message msg) {
		switch (msg.what) {
		case MSG_SHOW_PROGRESSE_DIALOG: {
			showProgressDialog(getString(R.string.update_loading));
			break;
		}
		case MSG_HIDE_PROGRESSE_DIALOG: {
			hideProgressDialog();
			break;
		}
		default:
			break;
		}
	}

}
